<?php

namespace app\api\controller;


use app\common\controller\Api;
use app\common\model\Attachment;
use app\common\model\Category;
use app\common\model\Dynamiclog;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\exception\ValidateException;
use think\Request;

/**
 * 咨询师发布动态
 */
class Dynamic extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];


    public function _initialize()
    {
        parent::_initialize();
        $this->goodsModel = new \app\admin\model\jeek\shop\SkuGoods;
        $this->dynamicModel = new \app\common\model\Dynamic;
    }

    /**
     * Account:咨询师发布动态
     * User: Ixiangang
     * Date: 2025/1/24
     * Time: 13:37
     */
    public function add(Request $request)
    {
        $user_id = $this->auth->id;
        $info = $this->goodsModel->where('user_id', $user_id)->find();
        if (empty($info)) {
            $this->error('咨询师信息不存在');
        }
        $content = $request->post('content');
        $images = $request->post('images');
        if (!$content || !$images) {
            $this->error("内容，图片不能为空");
        }
        $rs = $this->dynamicModel->insert([
            'user_id' => $user_id,
            'goods_id' => $info->id,
            'content' => $content,
            'images' => $images,
            'createtime' => time()
        ]);
        if ($rs) $this->success("发布成功");
        $this->error("发布失败");
    }


    /**
     * Account:咨询师我的动态
     * User: Ixiangang
     * Date: 2025/1/24
     * Time: 15:59
     */
    public function list()
    {
        $user_id = $this->auth->id;
        $list = $this->dynamicModel->getListPage(['user_id' => $user_id], $user_id);
        $this->success('请求成功', $list);
    }

    /**
     * Account:删除动态
     * User: Ixiangang
     * Date: 2025/1/24
     * Time: 15:09
     */
    public function delete(Request $request)
    {
        $dynamic_id = $request->post('dynamic_id');
        $info = $this->dynamicModel->where(['user_id' => $this->auth->id, 'id' => $dynamic_id])->find();
        if (empty($info)) {
            $this->error('动态不存在');
        }
        $del = $info->delete();
        if ($del) {
            $this->success("删除成功");
        }
        $this->error("删除失败");
    }


    /*********************--用户端---------------------------------------------------*/

    /**
     * Account:咨询师动态
     * User: Ixiangang
     * Date: 2025/2/6
     * Time: 10:58
     */
    public function goodsDynamic(Request $request)
    {
        $page = $request->post('page');
        $goods_id = $request->post('goods_id');
        if (!$goods_id) $this->error("参数错误");
        $list = $this->dynamicModel->getListPage(['goods_id' => $goods_id], $this->auth->id);
        $this->success('请求成功', $list);
    }

    /**
     * Account:动态详情
     * User: Ixiangang
     * Date: 2025/2/6
     * Time: 14:32
     */
    public function goodsDynamicDetails(Request $request)
    {
        $user_id = $this->auth->id;
        $dynamic_id = $request->post('dynamic_id');
        if (!$dynamic_id) $this->error("动态id不能空");
        $info = $this->dynamicModel->getInfo(['dynamic_id' => $dynamic_id],$user_id);
        $this->success("OK",$info);
    }

    /**
     * Account:点赞、取消点赞
     * User: Ixiangang
     * Date: 2025/2/6
     * Time: 13:30
     */
    public function give(Request $request)
    {
        $user_id = $this->auth->id;
        $type = $request->post('type'); //1=点赞，-1取消点赞
        $dynamic_id = $request->post('dynamic_id');
        if (!in_array($type, ['1', '-1'])) $this->error("参数错误");
        $dynamic = $this->dynamicModel->where(['id' => $dynamic_id])->find();
        if (empty($dynamic)) $this->error("咨询师动态不存在");
        if ($type == '1') {
            # 查询用户是否点赞过
            $dynamiclog = Dynamiclog::where(['user_id' => $user_id, 'dynamic_id' => $dynamic_id])->find();
            if (!empty($dynamiclog)) $this->success("请勿重复点赞");
            Dynamiclog::create([
                'user_id' => $user_id,
                'goods_id' => $dynamic->goods_id,
                'dynamic_id' => $dynamic_id,
                'type' => 1,
                'createtime' => time()
            ]);
            $this->dynamicModel->getUpdateGive($dynamic_id, '1');
            $this->success("点赞成功");
        } else {
            Dynamiclog::where(['user_id' => $user_id, 'dynamic_id' => $dynamic_id])->delete();
            $this->dynamicModel->getUpdateGive($dynamic_id, '-1');
            $this->success("取消点赞");
        }
        $this->error("点赞错误");
    }

    /**
     * Account:用户评价动态
     * User: Ixiangang
     * Date: 2025/2/6
     * Time: 14:15
     */
    public function comm(Request $request)
    {
        $user_id = $this->auth->id;
        $dynamic_id = $request->post('dynamic_id');
        $content = $request->post('content');
        if (!$content) $this->error("评价内容不能空");
        $dynamic = $this->dynamicModel->where(['id' => $dynamic_id])->find();
        if (empty($dynamic)) $this->error("咨询师动态不存在");
        Dynamiclog::create([
            'user_id' => $user_id,
            'goods_id' => $dynamic->goods_id,
            'dynamic_id' => $dynamic_id,
            'content' => $content,
            'type' => 2,
            'createtime' => time()
        ]);
        $this->dynamicModel->getUpdateComm($dynamic_id, 1);
        $this->success("评价成功");
    }


}